package com.example.spring.test;

/**
 * Created by Administrator on 2017-8-11.
 * ^~^，逻辑混乱类
 *
 */
class Foo {
    private int x=100;

    public int getX() {
        return x;
    }

    public int fix(int y){
        synchronized(this){
            x = x - y;
        }
        return x;
    }

}

public class MyThread implements Runnable{
    private Foo foo = new Foo();

    public static void main(String[] args) {
        MyThread t = new MyThread();
        Thread threadA = new Thread(t,"thread-A.");
        Thread threadB = new Thread(t,"thread-B.");

        threadA.start();
        threadB.start();
    }

    @Override
    public void run() {
        for(int i = 0; i < 3; i++){
            this.fix(30);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"：当前foo对象x的值："+foo.getX());
        }

    }

    public int fix(int y){
        return foo.fix(y);
    }
}
